import tkinter as tk
from tkinter import messagebox
from database import Database
from gui import LibraryGUI
from models import User, Book


class LibrarySystem:
    def __init__(self):
        self.db = Database()
        self.root = tk.Tk()
        self.current_user = None
        self.gui = LibraryGUI(self.root, self)

    def initialize_data(self):
        """初始化默认数据"""
        # 添加默认管理员
        admin = User(
            username="admin",
            password="admin123",
            role="admin",
            email="admin@library.com",
            phone="1234567890"
        )
        self.db.add_user(admin)

        # 添加默认图书
        books = [
            Book("Python编程", "张三", "9787111234567", "计算机", 5),
            Book("数据结构", "李四", "9787111234568", "计算机", 3),
            Book("算法导论", "王五", "9787111234569", "计算机", 2),
            Book("百年孤独", "马尔克斯", "9787111234570", "文学", 4),
            Book("时间简史", "霍金", "9787111234571", "科学", 3)
        ]

        for book in books:
            self.db.add_book(book)

    def login(self, username, password):
        """用户登录"""
        user = self.db.authenticate_user(username, password)
        if user:
            self.current_user = user
            return True, user.role
        return False, None

    def logout(self):
        """用户登出"""
        self.current_user = None

    def run(self):
        """运行系统"""
        self.root.title("图书管理系统")
        self.root.geometry("800x600")
        self.root.mainloop()


if __name__ == "__main__":
    system = LibrarySystem()
    system.initialize_data()
    system.run()